package com.lb.shopguide.config;

import android.content.Context;
import android.os.Environment;

import com.lb.shopguide.constants.AppConstant;
import com.lb.shopguide.entity.ShopBean;

import java.io.File;

/**
 * Created by zsd on 2016/12/13.
 */

public class AppConfigManager extends BaseSpConfig {
    /**
     * 配置文件SharedPreferences名称
     */
    private final static String SP_FILE_NAME = "com.lb.shopguide";
    public static String DIR_IMAGE;
    public static String DIR_DOWNLOAD;
    public static String DIR_PATCH;
    public static String DIR_APK;
    public static String DIR_LOG;
    public static String DIR_FILE;
    public static String DIR_APP_CACHE;
    private static AppConfigManager mAppConfigManager;

    /**
     * 开发模式 1：开发模式； 2：预发布模式 ； 3.正式发布模式
     */
//    private int devMode = AppConstant.KEY_RUN_MODE_DEVELOP;
    private int devMode = AppConstant.KEY_RUN_MODE_FORMAL;
//    private int devMode = AppConstant.KEY_RUN_ MODE_BETA;

    /**
     * 最新版本Code
     */
    private int versionCode;

    /**
     * 最新版本Name
     */
    private String versionName;

    private String downLoadApkUrl;

    private String usercode;

    private boolean isFirstTime;
    /**
     * 推送开关
     */
    private boolean pushToggle;

    private ShopBean shopBean;

    private long score = -1;
    /**
     * 配置文件SharedPreferences名称
     */
    private static String mSpFileName;

    private AppConfigManager(Context context) {
        super.initBaseConfig(context);
        mSpFileName = SP_FILE_NAME + ".preferences";
        initAppDir();
    }

    public static AppConfigManager getAppconfigManager(Context ctx) {
        if (mAppConfigManager == null) {
            mAppConfigManager = new AppConfigManager(ctx);
        } else if (mAppConfigManager.mContext == null) {
            mAppConfigManager.mContext = ctx;
        }
        return mAppConfigManager;
    }


    /**
     * 初始化应用存储目录
     * storage/emulated/0/Android/data/cn.cn.myclouedu.enterprise/files
     * storage/emulated/0/Android/data/cn.cn.myclouedu.enterprise/cache
     */
    private void initAppDir() {
        String cache_path = getCacheDir();
        String file_path = getFileDir();
        DIR_FILE = file_path.concat("/file");
        DIR_IMAGE = file_path.concat("/image");
        DIR_DOWNLOAD = file_path.concat("/download");
        DIR_LOG = cache_path.concat("/log");
        DIR_APK = file_path.concat("/apk");
        DIR_PATCH = file_path.concat("/patch");
        DIR_APP_CACHE = file_path.concat("/cache");
        String[] dirArr = {DIR_FILE, DIR_IMAGE, DIR_LOG, DIR_DOWNLOAD, DIR_APK, DIR_PATCH, DIR_APP_CACHE};
        for (int i = 0; i < dirArr.length; i++) {
            File _FileDir = new File(dirArr[i]);
            if (!_FileDir.exists()) {
                _FileDir.mkdirs();
            }
        }
    }

    /**
     * 获取文件路径
     *
     * @return
     */
    private String getCacheDir() {
        String cache_path = null;
        if (mContext.getExternalFilesDir(null) != null) {
            cache_path = mContext.getExternalCacheDir().getAbsolutePath();
        } else {
            cache_path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Android/data/" + mContext.getPackageName() + "/cache";
        }
        return cache_path;
    }

    /**
     * 获取缓存路径
     *
     * @return
     */
    private String getFileDir() {
        String cache_path = null;
        if (mContext.getExternalFilesDir(null) != null) {
            cache_path = mContext.getExternalFilesDir(null).getAbsolutePath();
        } else {
            cache_path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Android/data/" + mContext.getPackageName() + "/files";
        }
        return cache_path;
    }


    public int getDevMode() {
        return devMode;
    }

    public void setDevMode(int devMode) {
        this.devMode = devMode;
    }


    public int getVersionCode() {
        return versionCode;
    }

    public void setVersionCode(int version) {
        this.versionCode = version;
    }

    public String getVersionName() {
        return versionName;
    }

    public void setVersionName(String versionName) {
        this.versionName = versionName;
    }

    public String getDownLoadApkUrl() {
        return downLoadApkUrl;
    }

    public void setDownLoadApkUrl(String downLoadApkUrl) {
        this.downLoadApkUrl = downLoadApkUrl;
    }

    public ShopBean getShopBean() {
        return shopBean;
    }

    public void setShopBean(ShopBean shopBean) {
        this.shopBean = shopBean;
    }

    public long getScore() {
        return score;
    }

    public void setScore(long score) {
        this.score = score;
    }

    public boolean isPushToggle() {
        return getBoolean(mSpFileName, AppConstant.PRE_KEY_PUSH_TOGGLE, true);
    }

    public void setPushToggle(boolean pushToggle) {
        setBoolean(mSpFileName, AppConstant.PRE_KEY_PUSH_TOGGLE, pushToggle);
    }
}
